package com.bfs.service.wallet;

import com.netflix.hystrix.*;
import feign.Feign;
import feign.Target;
import feign.hystrix.SetterFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.lang.reflect.Method;

/**
 * @author baohuajie
 * @description: TODO
 * @date 2020/11/1411:05 AM
 */
@Configuration
public class WalletServiceFeignConfig {

    @Bean
    public SetterFactory setterFactory() {
        SetterFactory setterFactory = new SetterFactory() {
            @Override
            public HystrixCommand.Setter create(Target<?> target, Method method) {
                String groupKey = target.name();
                String commandKey = Feign.configKey(target.type(), method);
                HystrixCommandGroupKey hystrixCommandGroupKey = HystrixCommandGroupKey.Factory.asKey(groupKey);
                HystrixCommandKey hystrixCommandKey = HystrixCommandKey.Factory.asKey(commandKey);
                HystrixCommandProperties.Setter hystrixCommandProperties = HystrixCommandProperties.Setter().withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.THREAD)
                        .withExecutionTimeoutEnabled(true)
                        .withExecutionTimeoutInMilliseconds(4000);
                HystrixThreadPoolProperties.Setter threadPoolPro = HystrixThreadPoolProperties.Setter()
                        .withCoreSize(10)
                        .withMaximumSize(20)
                        .withKeepAliveTimeMinutes(5)
                        .withQueueSizeRejectionThreshold(100);

                HystrixCommand.Setter setter = HystrixCommand.Setter.
                        withGroupKey(hystrixCommandGroupKey)
                        .andCommandKey(hystrixCommandKey)
                        .andThreadPoolPropertiesDefaults(threadPoolPro)
                        .andCommandPropertiesDefaults(hystrixCommandProperties);
                return setter;
            }
        };

        return setterFactory;
    }


}
